const { ccclass, property } = cc._decorator;

@ccclass
export default class SoundManager {
  sound_path: string = 'sound/';
  sounds: { [key: number]: any } = {};
  enabled: boolean = true;
  music: string = '';
  protected static instance: SoundManager;
  public static getInstance(): SoundManager {
    // 单例模式
    if (!this.instance) {
      this.instance = new SoundManager();
    }
    return this.instance;
  }

  addSound(key: string, clip: cc.AudioClip) {
    this.sounds[key] = clip;
    cc.log(this.sounds)
  }

  playFx(fxName: string) {
    if (!this.enabled) return;
    cc.audioEngine.playEffect(this.sounds[fxName], false);
  }

  playMusic(musicName: string) {
    this.music = musicName;
    if (!this.enabled) return;
    cc.audioEngine.playMusic(this.sounds[musicName], true);
  }

  stopMusic() {
    cc.audioEngine.stopMusic();
  }

  setEnabled(enabled: boolean) {
    this.enabled = enabled;
    if (this.enabled) {
      this.playMusic(this.music);
    }
    else {
      cc.audioEngine.stopAll();
    }
  }

  getEnable() {
    return this.enabled;
  }
}
